package com.xdxc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdxc.model.dto.CorrectAnswerDTO;
import com.xdxc.model.entity.Answer;
import com.xdxc.model.entity.CorrectAnswer;
import com.xdxc.model.entity.Question;
import com.xdxc.response.Result;
import com.xdxc.service.CorrectAnswerService;
import com.xdxc.service.QuestionService;
import com.xdxc.service.impl.QuestionsService;
import com.xdxc.utils.CompletableFutureUtil;
import com.xdxc.vo.AnswerVo;
import com.xdxc.vo.QuestionWithOptionsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/correctAnswer")
public class CorrectAnswerController {

    @Autowired
    private CorrectAnswerService correctAnswerService;
    @Autowired

    private  QuestionService questionService;

    @PostMapping("/getCorrectAnswer")
    public Result<CorrectAnswerDTO> getCorrectAnswer(@RequestParam("questionId") Long questionId){

        Map<String, Object> relationData = CompletableFutureUtil.allOf(
                ()-> correctAnswerService.getOne(new LambdaQueryWrapper<CorrectAnswer>().eq(CorrectAnswer::getQuestionId,questionId)),
                ()-> questionService.getOne(
                        new LambdaQueryWrapper<Question>()
                                .select(Question::getContent) // 只查询指定字段
                                .eq(Question::getId, questionId)
                ).getContent()
        ).join();
        CorrectAnswer correctAnswer= (CorrectAnswer)relationData.get("data0");
        String content= (String) relationData.get("data1");



        CorrectAnswerDTO correctAnswerDTO  =CorrectAnswerDTO.builder()
                .answerVersion(correctAnswer.getAnswerVersion())
                .shortAnswer(correctAnswer.getShortAnswer())
                .optionIds(correctAnswer.getOptionIds())
                .fillKeywords(correctAnswer.getFillKeywords())
                .questionId(correctAnswer.getQuestionId())
                .questionContent(content)
                .build();
        return Result.success(correctAnswerDTO);
    }
}
